package com.andyliu.system.service.impl;

import com.andyliu.system.entity.Role;
import com.andyliu.system.entity.UserRole;
import com.andyliu.system.mapper.RoleMapper;
import com.andyliu.system.mapper.UserRoleMapper;
import com.andyliu.system.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author andyLiu
 * @since 2021-08-10
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

      public  IPage<Role> selectByPage(Integer pageNumber,Integer pageSize, Role role){
        return roleMapper
                .selectByPage(new Page<Role>(pageNumber,pageSize),role);
      }

    @Override
    public boolean saveUserRole(Integer userId, Integer roleId) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("role_id",roleId);
        userRoleMapper.delete(wrapper);
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId.longValue());
        userRole.setUserId(userId.longValue());
        int status = userRoleMapper.insert(userRole);
        return status==1?true:false;
    }
}
